package com.mashibing.class02;

/**
 * 异或运算的
 * 异或 : 相同为0 不同为1  异或运算就是无进位相加.
 * 异或运算满足交换律和结合律.
 * 异或性质:
 *         1) 0^N = N
 *         2) N^N = 0
 *         3) a^b^c = (a^b)^c=a^(b^c)
 */
public class Code01_Swap {
    public static void main(String[] args) {
        int a = 16;
        int b = 603;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        int[] arr = {3,1,100};
        int i = 0;
        int j = 0;
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
        System.out.println(arr[i] + " , " + arr[j]);
        System.out.println("arr[0] = " + arr[0]);
        System.out.println("arr[2] = " + arr[2]);
        swap(arr, 0, 0);
        System.out.println(arr[0]);
        System.out.println(arr[2]);
    }


    public static void swap (int[] arr, int i, int j) {
        // arr[0] = arr[0] ^ arr[0];
        arr[i]  = arr[i] ^ arr[j];
        arr[j]  = arr[i] ^ arr[j];
        arr[i]  = arr[i] ^ arr[j];
    }
}
